﻿using Entity;
using Furion.DynamicApiController;
using Furion.EventBus;

namespace Teambuy.Services
{
    public class MockServices : IDynamicApiController
    {
        private readonly Logics.Teambuy teambuy;
        private readonly IEventPublisher pub;
        private readonly ISqlSugarClient db;

        public MockServices(Logics.Teambuy teambuy, IEventPublisher pub, ISqlSugarClient db)
        {
            this.teambuy = teambuy;
            this.pub = pub;
            this.db = db;
        }

        /// <summary>
        /// 模拟结束拼团
        /// </summary>
        /// <returns></returns>
        public async Task<object> EndTeam()
        {
            var t = await db.Queryable<Entity.TeamBuy>().Where(x => x.State == TeamBuyState.进行中 && x.MaxNum == x.CurrentNum).FirstAsync();

            await teambuy.End(t.Id);

            return "ok";
        }

        /// <summary>
        /// 模拟订单支付成功
        /// </summary>
        /// <param name="orderNo"></param>
        /// <returns></returns>
        public async Task<object> OrderComplete(string orderNo)
        {
            await pub.PublishAsync("OrderComplete", new Events.Dto.OrderComplete
            {
                OrderNo = orderNo,

                ThreeOrderNo = "AABBCCDDEEFFGGHHIIJJKK",

                PayType = Entity.PayType.微信
            });

            return "ok";
        }

        public async Task<object> GetTeamUserPrice(int teamId)
        {
            // 查询拼团信息
            var teamBuy = await db.Queryable<Entity.TeamBuy>().FirstAsync(x => x.Id == teamId);

            var users = await teambuy.GetTeamUserLogAndMoney(teamId, teamBuy.ProductId);

            return users;
        }

        public async Task<object> RandomRedpacket(int usercount)
        {
            var me = App.GetService<Logics.Teambuy>();

            var res = me.RandomRedPackeet(18, usercount);

            return res;
        }
    }
}
